<?php
namespace app\common\lib\module_config\tagsconfig;

use app\common\lib\model_operate\Tools;
use app\common\lib\module_config\TagsConfig;
use app\common\lib\module_config\FormCtrl;
use think\Db;

//protected $config 配置信息
//protected $trace 追踪信息
//protected $tags 标签名
//protected $param 参数
//protected $rule 规则

/**
 * search标签类
 */
class Search extends TagsConfig
{
    private $param; //动态参数
    private $rule; //规则

    /**
     * 构造
     */
    public function __construct($trace, $tags)
    {
        parent::__construct($trace, $tags);
        //解析配置参数/规则信息
        $this->analyticConfig();
    }

    /**
     * 解析配置,获取参数和规则
     */
    private function analyticConfig()
    {
        //解析参数和规则
        if (!empty($this->fields)) {
            foreach ($this->fields as $key => $value) {
                //是否显示
                if ($value['is_show'] == 1) {
                    $this->param[$key] = $value['default'];
                    $this->rule[$key]  = $value['rule'];
                }
            }
        }

    }

    /**
     * 设置动态参数
     */
    public function setParam(array $param = [])
    {   
        
        $this->transformshow($param);

        if (!empty($this->param)) {
            //合并配置参数
            $this->param = array_merge($this->param, $param);
        } else {
            $this->param = $param;
        }
    }

    /**
     * 搜索参数处理
     */
    public function transformshow(&$param)
    {
        $config = $this->config['fields'];

        unset($param['_plugin'], $param['_controller'], $param['_action']);

        foreach ($param as $key => $value) {

            if (empty($config[$key])) {
                continue;
            }
            switch ($config[$key]['ctrl_type']) {
                case 'date':
                    $param[$key] = strtotime($param[$key]);
                    break;
                default:
                    # code...
                    break;
            }
        }
        return $param;
    }





    /**
     * 获取动态参数
     */
    public function getParam()
    {
        return $this->param;
    }

    /**
     *    获取规则
     */
    public function getRule()
    {
        return $this->rule;
    }

    /**
     * 创建Html
     */
    public function createHtml()
    {
        //生成的页面代码
        $html = '';
        $scr  = '';
        //页面中需要的js代码
        $scr .= "<script> window.onload=function(){";
        $param = $this->param;
        if (empty($this->fields)) {
            return "";
        }

        //遍历配置
        foreach ($this->fields as $key => $value) {
            //如果不显示则进入下一个循环
            if ($value['is_show'] == '2') {
                continue;
            }
            //如果有设置标题则添加标题
            if (!empty($value['title'])) {
                $html .= '<label style="padding:0px 1px;">' . $value['title'] . "</label>";
            }
            //类型为下拉框的情况
            if ($value['ctrl_type'] == 'select') {
                $html .= " <div class='layui-input-inline ' style='width: 100px;' >";
                $html .= " <select name='" . $key . "' id='" . $key . "'>";
                //show_type为1，从表中获取选项
                if ($value['show_type'] == '1') {
                    $html .= $this->selectGetTableData($value['option'], $param[$value['ctrl_name']]);
                    //下拉选择框的show_type为2，参数直接从json的option中获取
                } else if ($value['show_type'] == '2') {
                    foreach (json_decode($value['option'], true) as $k => $v) {
                        $html .= " <option value ='" . $k . "'";
                        if (isset($param[$key])) {
                            if ($param[$key] == $k) {
                                $html .= " selected ";
                            }
                        }
                        $html .= ">" . $v . "</option>";
                    }
                }
                $html .= " </select>";
                $html .= " </div>";
            }
            //类型为输入框的情况
            if ($value['ctrl_type'] == 'text') {
                $html .= " <div class='layui-input-inline ' style='width: 100px;' >";
                $html .= "<input type='text' id='" . $key . "' name='" . $key . "' placeholder='" . $value['placeholder'] . "' class='layui-input' autocomplete='off' value='";
                if (!empty($param[$key])) {
                    $html .= $param[$key];
                }
                $html .= "'>";
                $html .= " </div>";
            }
            //类型为日期选择的情况
            if ($value['ctrl_type'] == 'date') {
                $html .= " <div class='layui-input-inline ' style='width: 100px;' >";
                $html .= "<input type='text' id='" . $key . "' name='" . $key . "' placeholder='" . $value['placeholder'] . "' class='layui-input' autocomplete='off' value='";
                if (!empty($param[$key])) {
                    $html .= date('Y-m-d', $param[$key]);
                }
                $html .= "'>";
                $html .= " </div>";
                //layui重渲染，必须
                $scr .= "layui.use('laydate', function(){var laydate = layui.laydate;laydate.render({elem: '#" . $key . "' });}); ";
            }

            //如果是引入html
            if ($value['ctrl_type'] == 'htmlpage') {
                $html .= $this->yinruhtml($value,$param);
            }
        }

        $scr .= "}</script>";
        $html .= $scr;
        return $html;
    }


    public function yinruhtml(&$value,&$param)
    {
        $fo = new FormCtrl($this->trace,$this->tags);

        $fo->setList($param);

        $html = $fo->setHtmlpage($value);

        return $html;
    }





    /**
     * select类型为2时
     * 查表显示数据
     */
    private function selectGetTableData($option, $val)
    {
        $str    = '';
        $option = json_decode($option, true);
        if (!empty($option) && count($option) == 3) {
            //将数组中的值赋值给变量
            list($table, $fields, $items) = $option;
            //将cmf_tablename转换为tablename
            $table = Tools::tableNameFormat($table);
            $da    = Db::name($table)->field($fields)->select();

            foreach ($items as $key => $value) {
                //不是参数的情况
                if (false === strpos($key, '$') && false === strpos($value, '$')) {
                    $str .= '<option value="' . $key . '">' . $value . '</option>';
                } else {
                    foreach ($da as $arr) {
                        $arrKeys  = array_keys($arr);
                        $ik       = str_replace("$", '', $key);
                        $iv       = str_replace("$", '', $value);
                        $selected = '';
                        if ($val == $arr[$ik]) {
                            $selected .= " selected ";
                        }
                        $str .= '<option ' . $selected . '  value="' . $arr[$ik] . '">' . $arr[$iv] . '</option>';
                    }
                }
            }
        }
        return $str;
    }



    public function getApiSearch()
    {
        $where = 1;

        $config = $this->config;

        if (empty($config['fields'])) {
            return $where;
        }

        foreach ($config['fields'] as $key => $value) {

            if (empty($value['rule'])) {
                continue;
            }

            $where .= " and ".$value['rule'];
        }

        return $where;

    }


}
